package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.wx.home.vo.GoodsVo;
import com.cskaoyan.bean.wx.home.vo.GrouponVo;
import com.cskaoyan.bean.wx.home.vo.HotGoodsVo;
import com.cskaoyan.bean.wx.home.vo.NewGoodsVo;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kongwo
 * @description
 * @since 2022/05/05 20:23
 */

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    /**
     * 商品管理主页显示
     *
     * @param goodsId
     * @param goodsSn
     * @param name
     * @param param
     * @return
     */
    @Override
    public GoodsData getGoodsForPage(String goodsId, String goodsSn, String name, BaseParam param) {

        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        List<MarketGoods> list;
        if (limit == 0) {
            list = marketGoodsMapper.selectGoodsList("id", "asc", null, null, null);
            return GoodsData.data(list.size(), 0, 0, 0, list);
        }

        PageHelper.startPage(page, limit);
        list = marketGoodsMapper.selectGoodsList(sort, order, goodsId, goodsSn, name);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GoodsData.data(total, pages, limit, page, list);

    }

    /**
     * 添加商品
     *
     * @param goodsInfoBO
     */
    @Override
    public void addNewGoods(GoodsInfoBO goodsInfoBO) {
        MarketGoods goods = goodsInfoBO.getGoods();
        MarketGoodsAttribute[] attributes = goodsInfoBO.getAttributes();
        MarketGoodsProduct[] products = goodsInfoBO.getProducts();
        MarketGoodsSpecification[] specifications = goodsInfoBO.getSpecifications();
        goods.setRetailPrice(products[0].getPrice());
        marketGoodsMapper.insertSelective(goods);
        Integer id = goods.getId();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(id);
            marketGoodsAttributeMapper.insertSelective(attribute);
        }
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(id);
            marketGoodsSpecificationMapper.insertSelective(specification);
        }
        for (MarketGoodsProduct product : products) {
            product.setGoodsId(id);
            marketGoodsProductMapper.insertSelective(product);
        }
    }

    /**
     * 编辑商品信息
     *
     * @param goodsInfoBO
     */
    @Override
    public void updateGoods(GoodsInfoBO goodsInfoBO) {
        MarketGoods goods = goodsInfoBO.getGoods();
        MarketGoodsProduct[] products = goodsInfoBO.getProducts();
        MarketGoodsAttribute[] attributes = goodsInfoBO.getAttributes();
        MarketGoodsSpecification[] specifications = goodsInfoBO.getSpecifications();
        marketGoodsMapper.updateByPrimaryKeySelective(goods);
        for (MarketGoodsAttribute attribute : attributes) {
            if (attribute.getId() == null) {
                attribute.setGoodsId(goods.getId());
                marketGoodsAttributeMapper.insertSelective(attribute);
            } else {
                marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
            }
        }
        for (MarketGoodsSpecification specification : specifications) {
            marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }
        for (MarketGoodsProduct product : products) {
            marketGoodsProductMapper.updateByPrimaryKeySelective(product);
        }

    }

    /**
     * 逻辑删除商品即其规格参数
     *
     * @param marketGoods
     */
    @Override
    public void deleteGoods(MarketGoods marketGoods) {
        marketGoodsMapper.updateGoodsDeleted(marketGoods.getId());
        marketGoodsProductMapper.updateGoodsProductDeleted(marketGoods.getId());
        marketGoodsAttributeMapper.updateGoodsAttributeDeleted(marketGoods.getId());
        marketGoodsSpecificationMapper.updateGoodsSpecificationDeleted(marketGoods.getId());
    }

    /**
     * 商品回显
     *
     * @param id
     * @return
     */
    @Override
    public GoodsInfoBO getGoodsDetail(Integer id) {
        GoodsInfoBO goodsInfoBO = new GoodsInfoBO();
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer categoryId = marketGoods.getCategoryId();
        goodsInfoBO.setGoods(marketGoods);
        goodsInfoBO.setAttributes(marketGoodsAttributeMapper.selectByGoodsId(id));
        goodsInfoBO.setCategoryIds(new Integer[]{marketCategoryMapper.selectPidByPrimaryKey(categoryId), categoryId});
        goodsInfoBO.setProducts(marketGoodsProductMapper.selectByGoodsId(id));
        goodsInfoBO.setSpecifications(marketGoodsSpecificationMapper.selectByGoodsId(id));
        return goodsInfoBO;
    }

    /**
     * 获取类别
     *
     * @return
     */
    @Override
    public CatAndBrandVO getCatAndBrandList() {

        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        catAndBrandVO.setBrandList(marketBrandMapper.selectAll());
        List<CatAndBrandVOListParam> list = marketCategoryMapper.selectParentCategory();
        for (CatAndBrandVOListParam param : list) {
            param.setChildren(marketCategoryMapper.selectChildCategory(param.getValue()));
        }
        catAndBrandVO.setCategoryList(list);
        return catAndBrandVO;
    }

    @Override
    public boolean checkGoodsName(String name, Integer id) {

        return marketGoodsMapper.selectByGoodsName(name, id) > 0;
    }

    /*
     * 创建日期：2022/05/08 14:01
     ** 小程序首頁顯示
     * @return * @return: List<GrouponVo>
     * @author JinLu Zhou
     */
    @Override
    public List<GrouponVo> queryAllGroupon() {
        List<GrouponVo> grouponVos = marketGoodsMapper.queryAllGroupon();
        for (GrouponVo grouponVo : grouponVos) {
            BigDecimal retailPrice = grouponVo.getRetailPrice();
            BigDecimal grouponDiscount = grouponVo.getGrouponDiscount();
            grouponVo.setGrouponPrice(retailPrice.subtract(grouponDiscount));
        }
        return grouponVos;
    }

    @Override
    public List<HotGoodsVo> queryAllHotGoods() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsHotEqualTo(true);
        ArrayList<HotGoodsVo> hotGoodsVos = new ArrayList<>();
        for (MarketGoods marketGoods : marketGoodsMapper.selectByExample(marketGoodsExample)) {
            HotGoodsVo hotGoodsVo = new HotGoodsVo();
            hotGoodsVo.set(marketGoods);
            hotGoodsVos.add(hotGoodsVo);
        }

        return hotGoodsVos;
    }

    @Override
    public List<NewGoodsVo> queryAllNewGoods() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsNewEqualTo(true);
        ArrayList<NewGoodsVo> NewGoodsVos = new ArrayList<>();
        for (MarketGoods marketGoods : marketGoodsMapper.selectByExample(marketGoodsExample)) {
            NewGoodsVo newGoodsVo = new NewGoodsVo();
            newGoodsVo.set(marketGoods);
            NewGoodsVos.add(newGoodsVo);
        }

        return NewGoodsVos;
    }

    @Override
    public BaseRespVo queryAllGoodsType() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketGoods.size());
        return baseRespVo;
    }

}
